<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 17/2/20
 * Time: 21:12
 */
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
use Think\Upload;

class DocController extends AdminbaseController{

    public function index(){
        $where=array();
        $keyword=I("keyword");
        if(!empty($keyword)){
            $where["title"]=array("like","%$keyword%");
        }
        $count=M('doc')->where($where)->count();
        $page=$this->page($count,20);

        $posts=M('doc')->where($where)->limit($page->firstRow,$page->listRows)->order("create_at DESC")->select();
        $this->assign("page",$page->show());
        $this->assign("formget",array_merge($_GET,$_POST));
        $this->assign("posts",$posts);

        $this->display();
    }

    public function add(){
        $this->display();
    }

    public function add_post(){
        $title=I('title');
        $file=$_FILES['upload_thumb'];
        //上传文件
        if (!empty($file)) {
            $config = array(
                'rootPath'=>'./data/upload/',
                'savePath'=>'doc/',
                'subName'=>array('date','Ymd')
            );
            $upload=new Upload($config);
            $info=@$upload->uploadOne($file);
            if(!$info) {
                exit(json_encode(array('status'=>false,'message'=>$upload->getError())));
            }else{
                $download_url=$info['savepath'].$info['savename'];
                $data['title']=$title;
                $data['download_url']=$download_url;
                $data['create_at']=date('y-m-d h:i:s',time());
                $result=M('doc')->add($data);
                if($result){
                    exit(json_encode(array('status'=>true,'message'=>'添加成功')));
                }else{
                    exit(json_encode(array('status'=>true,'message'=>'添加失败')));
                }
            }
        }else{
            exit(json_encode(array('status'=>false,'message'=>'文件出现异常')));
        }
    }

    public function delete(){
        $id=I('id');
        //查找该比赛中有无照片墙
        $result=M('doc')->delete($id);
        if($result!==false){
            exit(json_encode(array("status"=>true,"message"=>"删除成功")));
        }else{
            exit(json_encode(array("status"=>false,"message"=>"删除失败")));
        }
    }

}